package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dto.LaheiDto;
import com.vf.admin.client.service.*;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppLaHeiController.java
 * @Description 拉黑接口
 * @createTime 2021年07月29日 10:10:00
 */
@Controller
@RequestMapping("/applahei")
public class AppLaHeiController extends ClientBaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AppRoomLaHeiService appRoomLaHeiService;
    @Autowired
    AppUserLaHeiService appUserLaHeiService;
    @Autowired
    AppUserDongtaiService appUserDongtaiService;
    @Autowired
    AppRoomService appRoomService;
    @Autowired
    AppUserService appUserService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    AppRoomAdminService appRoomAdminService;


    /**
     * @Description 用户拉黑
     * @Param: [lhuserid] 拉黑的用户id
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/07/29
     */
    @ResponseBody
    @RequestMapping("/userlahei")
    public Result userlahei(Integer lhuserid) {
        try {
            if (lhuserid == null) return this.errorJSON("ID不能为空");
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            if (user == null) return this.errorJSON("用户不存在");
            AppUser lhuser = appUserService.getTcOrId(lhuserid);
            if (user == null) return this.errorJSON("拉黑用户不存在");
            //获取是否已经拉黑
            AppUserLaHei appUserLaHei = appUserLaHeiService.getByUseridAndLhUserid(userId, lhuser.getId());
            if (appUserLaHei != null) {
                return this.errorJSON("您已拉黑该用户");
            }
            //添加拉黑用户信息
            appUserLaHei = new AppUserLaHei();
            appUserLaHei.setUserid(user.getId());           //用户id
            appUserLaHei.setPic(user.getPic());             //用户头像
            appUserLaHei.setNick(user.getNick());           //用户昵称
            appUserLaHei.setPhone(user.getLoginname());     //用户手机号
            appUserLaHei.setQianming(user.getMysign());
            appUserLaHei.setSex(user.getSex());
            appUserLaHei.setLhuserid(lhuser.getId());       //拉黑用户id
            appUserLaHei.setLhpic(lhuser.getPic());         //拉黑用户头像
            appUserLaHei.setLhnick(lhuser.getNick());       //拉黑用户昵称
            appUserLaHei.setLhphone(lhuser.getLoginname()); //拉黑用户电话
            appUserLaHei.setLhqianming(lhuser.getMysign());
            appUserLaHei.setLhsex(lhuser.getSex());
            appUserLaHei.setCreatetime(new Date());
            appUserLaHeiService.saveOrUpdate(appUserLaHei);
            return this.sucessJSON("成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("获取房间拉黑列表接口异常" + e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    /**
     * @Description 用户拉黑列表
     * @Param: [pageon] 页码
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/07/29
     */
    @ResponseBody
    @RequestMapping("/userlist")
    public Result userlist(Integer pageon) {
        try {
            if (pageon == null) return this.errorJSON("页码不能为空");
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            if (user == null)  return this.errorJSON("用户不存在");
            SplitPageBean splitPageBean = new SplitPageBean(pageon, 10);
            Map<String, Object> map = new HashMap<>(16);
            map.put("userid", userId);
            List<AppUserLaHei> appUserLaHeiList = appUserLaHeiService.findList(map, splitPageBean);
            //返回的数据
            List<LaheiDto> laheiDtoList = new ArrayList<>();
            for (AppUserLaHei appUserLaHei : appUserLaHeiList) {
                LaheiDto laheiDto = new LaheiDto();
                laheiDto.setUserid(appUserLaHei.getLhuserid());//用户ID
                laheiDto.setNick(appUserLaHei.getLhnick());
                laheiDto.setPic(appUserLaHei.getLhpic());//头像
                laheiDto.setQianming(appUserLaHei.getLhqianming());//签名
                laheiDto.setSex(appUserLaHei.getLhsex());//性别
                laheiDtoList.add(laheiDto);
            }
            return this.sucessPage(laheiDtoList, splitPageBean.getNextPageNo());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("获取用户拉黑列表接口异常" + e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    /**
     * @Description 用户取消拉黑
     * @Param: [lhuserid] 拉黑的用户id
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/07/29
     */
    @ResponseBody
    @RequestMapping("/qxuserlahei")
    public Result qxuserlahei(Integer lhuserid) {
        try {
            if (lhuserid == null) return this.errorJSON("拉黑用户ID不能为空");
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            if (user == null) return this.errorJSON("用户不存在");
            AppUser lhuser = appUserService.get(lhuserid);
            if (lhuser == null) return this.errorJSON("拉黑用户不存在");
            //获取是否已经拉黑
            AppUserLaHei appUserLaHei = appUserLaHeiService.getByUseridAndLhUserid(userId, lhuserid);
            if(appUserLaHei == null){
                return this.errorJSON("未拉黑该用户");
            }
            appUserLaHeiService.delete(appUserLaHei.getId());
            return this.sucessJSON("成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("获取房间拉黑列表接口异常" + e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    /**
     * @Description 不看ta的动态
     * @Param: [gbuserid] 不看ta的动态的用户id
     * @return: com.vf.admin.common.entity.Result
     * @Author: xzx
     * @Date: 2020/10/29
     */
    @ResponseBody
    @RequestMapping("/noLookUdongtai")
    public Result noLookUdongtai(Integer gbuserid) {
        try {
            if (gbuserid == null) {
                return this.errorJSON("关闭的用户ID为空！");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            if (user == null) {
                return this.errorJSON("用户不存在！");
            }
            AppUser gbuser = appUserService.get(gbuserid);
            if (gbuser == null) {
                return this.errorJSON("关闭动态的用户不存在！");
            }
            //获取是否已经拉黑
            AppUserDongtai appUserDongtai = appUserDongtaiService.getByUseridAndGbUserid(userId, gbuserid);
            if (appUserDongtai != null) {
                return this.errorJSON("你已经关闭该用户的动态,不可重复操作！");
            }
            //添加不看ta的动态用户信息
            appUserDongtai = new AppUserDongtai();
            appUserDongtai.setUserid(user.getId());//用户id
            appUserDongtai.setPic(user.getPic());//用户头像
            appUserDongtai.setNick(user.getNick());//用户昵称
            appUserDongtai.setPhone(user.getLoginname());//用户手机号
            appUserDongtai.setGbuserid(gbuser.getId());//关闭动态用户id
            appUserDongtai.setGbpic(gbuser.getPic());//关闭动态用户头像
            appUserDongtai.setGbnick(gbuser.getNick());//关闭动态用户昵称
            appUserDongtai.setGbphone(gbuser.getLoginname());//拉黑用户电话
            appUserDongtai.setCreatetime(new Date());
            appUserDongtaiService.saveOrUpdate(appUserDongtai);
            return this.sucessJSON("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("不看ta的动态异常" + e);
            return this.errorJSON("系统繁忙,请联系管理员！");
        }
    }

    /**
     * @Description 取消不看ta的动态
     * @Param: [gbuserid] 不看ta的动态的用户id
     * @return: com.vf.admin.common.entity.Result
     * @Author: xzx
     * @Date: 2020/10/29
     */
    @ResponseBody
    @RequestMapping("/quxiaoNoLookUdongtai")
    public Result quxiaoNoLookUdongtai(Integer gbuserid) {
        try {
            if (gbuserid == null) {
                return this.errorJSON("取消不看ta的动态的用户ID为空！");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            if (user == null) {
                return this.errorJSON("用户不存在！");
            }
            AppUser gbuser = appUserService.get(gbuserid);
            if (gbuser == null) {
                return this.errorJSON("取消不看ta的动态的用户不存在！");
            }
            //获取是否已经拉黑
            AppUserDongtai appUserDongtai = appUserDongtaiService.getByUseridAndGbUserid(userId, gbuserid);
            if (appUserDongtai == null) {
                return this.errorJSON("你未设置不看ta的动态,不可重复操作！");
            }
            //删除不看ta的动态用户信息
            appUserDongtaiService.delete(appUserDongtai.getId());
            return this.sucessJSON("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("取消不看ta的动态异常" + e);
            return this.errorJSON("系统繁忙,请联系管理员！");
        }
    }
}
